<?php
class ml_model_dbUserfeed extends Lib_datamodel_db
{
    function __construct() {
        $db_config = ml_factory::load_standard_conf ( 'dbContent' );
        parent::__construct ( 'userfeed', $db_config ['userfeed'] );
    }

    protected function hook_after_fetch()
    {
        $data = $this->get_data();
        if(isset($data['feed_info']))
        {
            $data['feed_info'] = unserialize($data['feed_info']);
            $this->set_data($data);
        }else
        {
            foreach($data as $key=>$val)
            {
                if(isset($val['feed_info']))
                {
                    $val['feed_info'] =unserialize($val['feed_info']);
                }
                $arr[$key]=$val;
            }
            $this->set_data($arr);
        }
        return true;
    }

    protected function hook_before_write($array)
    {
        if(isset($array['feed_info']))
        {
            $array['feed_info'] = serialize($array['feed_info']);
        }
        return $array;
    }

    public function getContentRid_by_rid($rid)
    {
        $uid = ml_tool_resid::resid2uid($rid);
        if(!$this->init_db($uid , self::DB_SLAVE  ))
        return false;
        $sql = 'select content_rid from `'.$this->table.'` where `rid`='.'\''.$rid.'\'';
        $rs=$this->fetch_row($sql);
        return $rs?$this->_data:false;
    }
    
    public function getTypeByRid($rid)
    {
        $uid = ml_tool_resid::resid2uid($rid);
        if(!$this->init_db($uid , self::DB_SLAVE  ))
        return false;
        $sql = 'select Type from `'.$this->table.'` where `rid`='.'\''.$rid.'\'';
        $rs=$this->fetch_row($sql);
        return $rs?$this->_data:false;
    }
    
    
    public function insert_content($data){
        $uid = $data['uid'];
        if(!$this->init_db($uid , self::DB_MASTER  ))
        return false;
        $array['rid'] = $data['rid'];
        $array['uid'] = $data['uid'];
        $array['type'] = $data['type'];
        $array['content'] = $data['content'];
        $array['pic_id'] = $data['pic_id'];
        $array['feed_info'] = $data['feed_info'];
        $array['from'] = empty($data['from']) ? ML_FROM_WEB : $data['from'];
        $array['content_rid'] = $data['content_rid'];
        $array['status'] = 1;
        $array['ctime'] = $data['ctime'];
        $array['cip'] = $data['cip'];
        return $this->insert($array);
    }

    public function del_contentByRid($rid){
        $uid = ml_tool_resid::resid2uid($rid);
        if(!$this->init_db($uid , self::DB_MASTER  ))
        return false;
        return $this->update(array('status' => 9), '`rid` = "'.$rid.'"', 1);
    }

    public function del_contentByContent_rid($content_rid){
        $uid = ml_tool_resid::resid2uid($content_rid);
        if(!$this->init_db($uid , self::DB_MASTER  ))
        return false;
        return $this->update(array('status' => 9), '`content_rid` = "'.$content_rid.'"', 1);

    }

    /**
     *
     * 查找$num个feed
     * @param unknown_type $uid
     * @param unknown_type $begin 起始位置，默认0
     * @param unknown_type $num 条目数，默认20，结尾-1
     * @param unknown_type $att 态度值，默认全部
     * @todo 根据类型
     */
    public function getFeedByUid($uid, $begin = 0, $num = ML_FEED_PAGESIZE,$type=null)
    {
        if(!$this->init_db($uid, self::DB_SLAVE ))
        return false;

        
        $limit = ' ORDER BY ctime DESC LIMIT '.$begin.','.$num;
        $whereAtt = isset($type)? ' AND status!=9 AND `type` ='.$type: ' and status!=9 ';
        $sql = 'SELECT *   FROM '. $this->table . ' WHERE uid = '. $uid. $whereAtt. $limit;
        
        $rs = $this->fetch($sql);
       
        return $rs?$this->_data:null;

    
    }

    public function getCountByUid($uid, $type=null)
    {
        if(!$this->init_db($uid))
        return false;

        $where = isset($type)? ' AND status!=9  AND `type` ='.$type: ' AND status!=9 ';
        
        $sql = 'SELECT COUNT(*) as  `num` FROM '. $this->table. ' WHERE uid = '. $uid. $where;
        
        $rs = $this->fetch_row($sql);
        
        return $rs? $this->_data: false;
    }
    
    public function getFeedByRid($rid){
        
        $uid=ml_tool_resid::resid2uid($rid);
        if(!$this->init_db($uid))
            return false;
        $sql = 'SELECT  *  FROM '. $this->table. ' WHERE `rid` = "'.$rid.'" ';
        $rs = $this->fetch_row($sql);
            return $rs? $this->_data: false;
    }
    
    public function getUserfeedByRids($aRids)
    {
        if(empty($aRids)){
            return null;                                        //参数错误
        }

        foreach ($aRids as $rid)
        {
            $aUid2Rid[ml_tool_resid::resid2uid($rid)][] = $rid;
        }

        $data=array();                                        //内容数据
        foreach ($aUid2Rid as $uid => $rids) {

            $this->init_db($uid);
            $sRid = '"'.implode('","' , $rids).'"';
            $sql = 'select * from `'.$this->table.'` where `rid` in ('.$sRid.') order by ctime desc';
            $rs = $this->fetch($sql);
            $data[]=$rs? $this->_data: null;
        }
        $arr=array();
        foreach ($data as $value) {
            if($value)
            $arr=array_merge($arr,$value);
        }
        $data = Tool_array::format_2d_array($arr , 'rid' , Tool_array::FORMAT_FIELD2ROW);
        return $data;
    }
    
    
    
    public function getUserfeedByRid($rid)
    {
        if(empty($rid))
        return null;                          
        
        $uid = ml_tool_resid::resid2uid($rid);
        
        if(!$this->init_db($uid))
        return false;

        $sql = 'select * from '.$this->table." where rid = '". $this->escape($rid). "'";
        $rs = $this->fetch($sql);
    
        $this->_data = $this->_data[0];
        return $rs;
    }
    
    /**
     * 根据content_rid找出userfeed记录
     * @param unknown_type $rid
     * @zhanglei11
     */
    public function getByContentRid($rid)
    {
        if(empty($rid))
            return null;
    
        $uid = ml_tool_resid::resid2uid($rid);
    
        if(!$this->init_db($uid))
            return false;
    
        $sql = 'select * from '.$this->table." where `content_rid` = '". $this->escape($rid). "'";
        $rs = $this->fetch($sql);
    
        $this->_data = $this->_data[0];
        return $rs;
    }
    
    /**
     * 根据表态查找feed 
     *
     * @param unknown_type $uid
     * @param unknown_type $con_rid
     * @return unknown
     */
    public function getFeedByAtt($uid,$con_rid){
        
        if(!$this->init_db($uid))
            return false;
            
        $sql = 'select * from '.$this->table." where `uid` = ". $this->escape($uid). " AND  `type`=6  AND  `content_rid` = '". $this->escape($con_rid). "'";
        return  $this->fetch($sql);
    }
    
}
?>